
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-11-25 11:49:21
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-31 09:49:22
 */

import { soliderType } from "../eles/baseEle";
import { Facade } from "../libs/pureMvc/patterns/facade/Facade";
import { Dictionary } from "../utils/dictionary";
import barrackCtr from "./barracks/barrackCtr";
import helpCtr from "./helps/helpCtr";
import victoryEntity from "./victory/victoryEntity";

export enum modelType {
  pve = 1, //冒险模式
  pvp = 2  //对战模式
}

export default class gameCtr {

  private static _ins: gameCtr = null;

  public static get ins(): gameCtr {
    if (this._ins == null) {
      this._ins = new gameCtr();
    }
    return this._ins;
  }

  public poolObj: cc.NodePool[] = []; //对象池数组

  public preObj: cc.Prefab[] = []; //预制体数组

  private _gold: number = 1200; //金币数量

  public mouseCurUrl: string = "ele/HandCursorIdle"; //鼠标图片

  public selectData: any = null; //选择数据

  public curScene: cc.Node = null; //当前 主场景

  public isOver: boolean = false;

  public score: number = 0; //积分

  public monsterInter: number = 350; //出怪间隔

  public isFire: boolean = false; //是否着火

  public isMusic: boolean = true;

  public fireIntel: number = 0; //

  public curretRanks: number = 1; //当前队伍

  public ranksArr: any[] = [
    [1, 2, 3, 4, 5, 6],
    [1, 0, 0, 0, 0, 0],
    [1, 0, 0, 0, 0, 0],
    [1, 0, 0, 0, 0, 0],
    [1, 0, 0, 0, 0, 0],
    [1, 0, 0, 0, 0, 0],
  ];

  public roleId: number = 0; //用户id

  public nickName: string = ""; //用户昵称

  public avatar: string = ""; //头像

  public diamonds: number = 0; //钻石数量


  /**
   * 所有兵种
   */
  public barracksList = [
    { id: 1, iconUrl: "ele/miner", harvest: 1, attack: 0, hp: 1, need: 25, name: "矿工", desc: "采集资源但是没有攻击力", namePos: cc.v2(-250, 80), poolName: "miner", lock: 0, isLock: false, quality: 1, isWar: false },
    { id: 2, iconUrl: "ele/spearman", harvest: 0, attack: 1, hp: 1, need: 55, name: "枪兵", desc: "会再三点间巡逻的枪兵", namePos: cc.v2(-150, 80), poolName: "spearman", lock: 0, quality: 1, isWar: false, isLock: false },
    { id: 3, iconUrl: "ele/guard", harvest: 0, attack: 1, hp: 20, need: 100, name: "守卫", desc: "绕圆巡逻的守卫", namePos: cc.v2(-50, 80), poolName: "guard", lock: 0, quality: 1, isWar: false, isLock: false },
    { id: 4, iconUrl: "ele/shield", harvest: 0, attack: 1, hp: 15, need: 130, name: "盾兵", desc: "以城堡为起点，两点间巡逻的盾兵", namePos: cc.v2(50, 80), poolName: "shield", lock: 0, quality: 1, isWar: false, isLock: false },
    { id: 5, iconUrl: "ele/cannon", harvest: 0, attack: 1, hp: 10, need: 200, name: "加农炮", desc: "每两秒发射一发，一发最多对2个敌人造成伤害", namePos: cc.v2(150, 80), poolName: "cannon", lock: 0, quality: 1, isWar: false, isLock: false },
    { id: 6, iconUrl: "ele/bomb", harvest: 0, attack: 1, hp: 1, need: 100, name: "炸弹", desc: "拖动到敌人密集处释放，最多对10个敌人造成伤害", namePos: cc.v2(250, 80), poolName: "bomb", isWar: false, isLock: false },
    // { id: 7, iconUrl: "ele/shell", harvest: 0, attack: 1, hp: 1, need: 50, name: "地雷", desc: "地雷", namePos: cc.v2(300, 80), poolName: "shell", lock: 0, quality: 1, isWar: false, isLock: false },

    { id: 7, iconUrl: "ele/militia", harvest: 0, attack: 1, hp: 3, need: 80, name: "民兵", desc: "双点直线", namePos: cc.v2(-250, 80), poolName: "militia", lock: 1000, quality: 2, isWar: false, isLock: true },
    { id: 8, iconUrl: "ele/bowman", harvest: 0, attack: 1, hp: 1, need: 150, name: "弓兵", desc: "圆形/100", namePos: cc.v2(-150, 80), poolName: "bowman", lock: 1000, attackAare: 300, quality: 2, isWar: false, isLock: true },
    { id: 9, iconUrl: "ele/swordsman", harvest: 0, attack: 1, hp: 3, need: 80, name: "剑士", desc: "圆形/200,攻击距离100", namePos: cc.v2(-50, 80), poolName: "swordsman", lock: 1000, attackAare: 100, quality: 2, isWar: false, isLock: true },

    { id: 10, iconUrl: "ele/cavalry", harvest: 0, attack: 1, hp: 8, need: 100, name: "骑兵", desc: "圆形/300", namePos: cc.v2(50, 80), poolName: "cavalry", lock: 1500, attackAare: 0, speedXs: 2, quality: 3, isWar: false, isLock: true },
    { id: 11, iconUrl: "ele/trapped", harvest: 0, attack: 1, hp: 100, need: 100, name: "陷阵骑士", desc: "朝一个方向不断前进，直至死亡", namePos: cc.v2(150, 80), poolName: "trapped", lock: 1500, attackAare: 0, quality: 3, isWar: false, isLock: true },
    { id: 12, iconUrl: "ele/dogs", harvest: 1, attack: 1, hp: 5, need: 100, name: "训练犬", desc: "挖掘地雷训练犬", namePos: cc.v2(250, 80), poolName: "dogs", lock: 1500, attackAare: 0, speedXs: 1.5, quality: 3, isWar: false, isLock: true },

    { id: 13, iconUrl: "ele/master", harvest: 0, attack: 1, hp: 2, need: 200, name: "法师", desc: "发射火球，攻击3个单位", namePos: cc.v2(300, 80), poolName: "master", lock: 2000, attackAare: 300, speedXs: 1, quality: 4, isWar: false, isLock: true },
    { id: 14, iconUrl: "ele/fighter", harvest: 0, attack: 1, hp: 10, need: 150, name: "格斗家", desc: "方形/400", namePos: cc.v2(300, 80), poolName: "fighter", lock: 2000, attackAare: 50, speedXs: 1, quality: 4, isWar: false, isLock: true },
    { id: 15, iconUrl: "ele/warlock", harvest: 0, attack: 1, hp: 1, need: 100, name: "术士", desc: "方形/400", namePos: cc.v2(300, 80), poolName: "warlock", lock: 2000, attackAare: 50, speedXs: 1, quality: 4, isWar: false, isLock: true },

    { id: 16, iconUrl: "ele/scarecrow", harvest: 0, attack: 1, hp: 20, need: 50, name: "荆棘草人", desc: "发射火球，攻击3个单位", namePos: cc.v2(300, 80), poolName: "scarecrow", lock: 3000, attackAare: 300, speedXs: 1, quality: 5, isWar: false, isLock: true },
    { id: 17, iconUrl: "ele/jumpFire", harvest: 0, attack: 1, hp: 120, need: 300, name: "跳跃之火", desc: "方形/400", namePos: cc.v2(300, 80), poolName: "jumpFire", lock: 3000, attackAare: 50, speedXs: 1, quality: 5, isWar: false, isLock: true },
    { id: 18, iconUrl: "ele/logistics", harvest: 0, attack: 1, hp: 1, need: 100, name: "后勤兵", desc: "方形/400", namePos: cc.v2(300, 80), poolName: "logistics", lock: 3000, attackAare: 50, speedXs: 1, quality: 5, isWar: false, isLock: true },
  ]

  /**
   * 宝箱列表
   */
  public boxList = [
    { id: 1, name: "铁箱", num: 1, step: 0, downTime: 60, reward: { num: 100, name: "钻石", iconUrl: "" } },
    { id: 2, name: "银箱", num: 0 },
    { id: 3, name: "金箱", num: 0 }
  ]

  /**
   * 城堡数据
   */
  public castleData = {
    attack: 1, harvest: 0, hp: 100, type: soliderType.castle, isCanMove: false, poolName: "castle"
  }

  /**
   * 资源数据
   */
  public resData = [
    { iconUrl: "ele/mineral_1", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-400, 145), poolName: "mineral", type: soliderType.mineral },
    { iconUrl: "ele/mineral_2", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-315, 199), poolName: "mineral", type: soliderType.mineral },
    { iconUrl: "ele/mineral_3", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-320, 110), poolName: "mineral", type: soliderType.mineral },

    { iconUrl: "ele/tree_1", harvest: 0, attack: 0, hp: 10, namePos: cc.v2(-200, 190), poolName: "tree", type: soliderType.tree },

    { iconUrl: "ele/dl", harvest: 0, attack: 0, hp: 10, namePos: cc.v2(-200, 190), poolName: "dl", type: soliderType.dl },

    { iconUrl: "ele/tree_2", harvest: 0, attack: 0, hp: 10, namePos: cc.v2(-144, -190), poolName: "tree", type: soliderType.tree },
    { iconUrl: "ele/tree_3", harvest: 0, attack: 0, hp: 10, namePos: cc.v2(144, 190), poolName: "tree", type: soliderType.tree },
    { iconUrl: "ele/mineral_1", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-435, 145), poolName: "mineral", type: soliderType.mineral },
    { iconUrl: "ele/mineral_2", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-315, 199), poolName: "mineral", type: soliderType.mineral },
    { iconUrl: "ele/mineral_3", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-320, 110), poolName: "mineral", type: soliderType.mineral },

    { iconUrl: "ele/mineral_1", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-435, 145), poolName: "mineral", type: soliderType.mineral },
    { iconUrl: "ele/mineral_2", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-315, 199), poolName: "mineral", type: soliderType.mineral },
    { iconUrl: "ele/mineral_3", harvest: 0, attack: 0, hp: 50, namePos: cc.v2(-320, 110), poolName: "mineral", type: soliderType.mineral }
  ]

  public resNode: cc.Node = null; //资源节点

  public eleNode: cc.Node = null; //元素节点

  public effectNode: cc.Node = null; //buff 节点

  public eleId: number = 0;

  public isMoveTriangle: boolean = false; //移动三角

  public gameTime: number = 0; //游戏时间

  public waveNum: number = 0;

  public dlTime: number = 0; //地雷生成时间

  public dlNum: number = 0; //地雷上限

  public resTime: number = 0;//生成资源间隔

  public gameModel: modelType = 1; //游戏类型

  public Integral: number = 0; //段位积分

  public guideId: number = 2; // 引导id

  public victoryNum: number = 0; //pvp累胜

  public victoryDic: Dictionary<any, victoryEntity> = null;

  public pvpList: any[] = [];
  public myPvpRank = 99;
  public mxList: any[] = [];
  public myMxRank = 99;
  public wPvpList: any = [];
  public myWPvpRank = 99;
  public wMxList: any = [];
  public myWMxRank = 99;
  public rankKey = "1";
  public rankKey2 = "1";

  public initTp(victoryJson) {
    let victoryEny: victoryEntity;
    this.victoryDic = new Dictionary();
    for (let val of victoryJson) {
      victoryEny = new victoryEntity();
      victoryEny.initTp(val);
      this.victoryDic.set(victoryEny.id, victoryEny);
    }
  }

  /**
   * 初始化累胜宝箱数据
   * @param val 
   */
  public initVictoryData(val) {
    // console.log(val);
    for (let v of val) {
      this.victoryDic.get(v.victoryId).isRecive = v.isRecive;
    }
  }

  /**
   * 段位数据
   */
  public rankRes = [
    { id: 1, name: "黑铁", need: 0, nextIntegral: 200, iconUrl: "icon/icon_8" },
    { id: 2, name: "白银", need: 200, nextIntegral: 500, iconUrl: "icon/icon_7" },
    { id: 3, name: "白金", nedd: 500, nextIntegral: 1000, iconUrl: "icon/icon_6" },
    { id: 4, name: "黄金", need: 1000, nextIntegral: 2000, iconUrl: "icon/icon_5" },
    { id: 5, name: "钻石", need: 2000, nextIntegral: 4000, iconUrl: "icon/icon_4" },
  ]

  /**
   * 获取段位名称
   */
  public get rankTxt() {
    for (let i = 0; i < this.rankRes.length; i++) {
      if (this.Integral >= this.rankRes[i].need && this.Integral < this.rankRes[i].nextIntegral) {
        return this.rankRes[i].name;
      }
    }
  }

  public get lvIcon() {
    for (let i = 0; i < this.rankRes.length; i++) {
      if (this.Integral >= this.rankRes[i].need && this.Integral < this.rankRes[i].nextIntegral) {
        return this.rankRes[i].iconUrl;
      }
    }
  }

  /**
   * 
   */
  public get rankPro() {
    for (let i = 0; i < this.rankRes.length; i++) {
      if (this.Integral >= this.rankRes[i].need && this.Integral < this.rankRes[i].nextIntegral) {
        return this.Integral / this.rankRes[i].nextIntegral;
      }
    }
  }

  public get gold() {
    return this._gold;
  }

  public set gold(val) {
    this._gold = val;
    this.curScene.emit("updateGold");
  }

  /**
   * 初始化对象池
   */
  public initPool(poolName: string = "", num: number = 5, pre: cc.Prefab) {
    if (this.poolObj[poolName]) return;

    if (!this.preObj[poolName]) {
      this.preObj[poolName] = pre;
    }
    this.poolObj[poolName] = new cc.NodePool(poolName);
    for (let i = 0; i < num; i++) {
      this.poolObj[poolName].put(cc.instantiate(pre));
    }
  }

  /**
   * 获取当前兵营数据
   */
  public get currentBarracks() {
    let arr1 = [];
    let arr2 = [];
    let ranksArr = this.ranksArr[Number(this.curretRanks) - 1];
    let list = barrackCtr.ins.barrackDic.getValueList();
    list.forEach(ele => {
      let bool = false;
      ranksArr.forEach(element => {
        if (ele.id == element) {
          bool = true;
          ele.isWar = true;
          arr1.push(ele);
        }
      });
      if (!bool) {
        ele.isWar = false;
        arr2.push(ele);
      }
    })

    arr1 = arr1.concat(arr2);

    return arr1;
  }

  /**
   * 当前队伍是否已经满
   */
  public isFull() {
    let isFull = true;
    let ranksArr = this.ranksArr[Number(this.curretRanks) - 1];
    ranksArr.forEach(ele => {
      if (ele == 0) {
        isFull = false;
      }
    })
    return isFull;
  }

  /**
   * 切换战斗和休息
   */
  public setWarRest(id, isWar: boolean = false) {
    let ranksArr = this.ranksArr[Number(this.curretRanks) - 1];

    for (let i = 0; i < ranksArr.length; i++) {
      if (isWar) {
        if (ranksArr[i] == 0) {
          ranksArr[i] = id;
          break;
        }
      } else {
        if (ranksArr[i] == id) {
          ranksArr[i] = 0;
          break;
        }
      }
    }
    console.log(this.ranksArr);
  }


  /**
 * 兵营
 */
  public get barracksArr() {
    let arr1 = [];
    let ranksArr = this.ranksArr[Number(this.curretRanks) - 1];
    let list = barrackCtr.ins.barrackDic.getValueList();
    list.forEach(ele => {
      let bool = false;
      ranksArr.forEach(element => {
        if (ele.id == element) {
          bool = true;
          ele.isWar = true;
          arr1.push(ele);
        }
      });
    })
    return arr1;
  }

}